home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gnome-python.idb / usr / freeware / lib / python1.5 / site-packages / GtkExtra.py.z / GtkExtra.py
Encoding:
Python Source  |  1999-07-16  |  6.5 KB  |  250 lines

  1. #
  2. # This file contains some convenience routines for gtk.py.
  3. # It includes a python implementation of the GtkMenuFactory, and
  4. # the functions message_box, file_open_box and file_save_box create
  5. # and manage the respective dialogs.
  6. #
  7.  
  8. from gtk import *
  9. import string
  10.  
  11. MENU_FACTORY_MENU_BAR    = 0
  12. MENU_FACTORY_MENU        = 1
  13. MENU_FACTORY_OPTION_MENU = 2
  14.  
  15. # type is MENU_FACTORY_{MENU,MENU_BAR,OPTION_MENU}
  16. class MenuFactory:
  17.     def __init__(self, type=MENU_FACTORY_MENU_BAR):
  18.         self.accelerator = GtkAccelGroup()
  19.         if type == MENU_FACTORY_MENU_BAR:
  20.             self.__w = GtkMenuBar()
  21.             self.__ret = self.__w
  22.         elif type == MENU_FACTORY_MENU:
  23.             self.__w = GtkMenu()
  24.             self.__w.set_accel_group(self.accelerator)
  25.             self.__ret = self.__w
  26.         elif type == MENU_FACTORY_OPTION_MENU:
  27.             self.__w = GtkMenu()
  28.             self.__w.set_accel_group(self.accelerator)
  29.             self.__ret = GtkOptionMenu()
  30.             self.__ret.set_menu(self.__w)
  31.         self.__menus = {}
  32.         self.__items = {}
  33.     def __getattr__(self, key):
  34.         return getattr(self.__ret, key)
  35.     def add_entries(self, entries):
  36.         for entry in entries:
  37.             apply(self.create, tuple(entry))
  38.     def create(self, path, accelerator=None, callback=None, *args):
  39.         last_slash = string.rfind(path, '/')
  40.         if last_slash < 0:
  41.             parentmenu = self.__w
  42.         else:
  43.             parentmenu = self.get_menu(path[:last_slash])
  44.         label = path[last_slash+1:]
  45.         if label == '<separator>':
  46.             item = GtkMenuItem()
  47.         elif label[:7] == '<check>':
  48.             item = GtkCheckMenuItem(label[7:])
  49.         else:
  50.             item = GtkMenuItem(label)
  51.         if label != '<nothing>':
  52.             item.show()
  53.         if accelerator:
  54.             key, mods = self.parse_accelerator(accelerator)
  55.             item.add_accelerator("activate", self.accelerator,
  56.                          key, mods, 'visible')
  57.         if callback:
  58.             apply(item.connect, ("activate", callback) + args)
  59.         # right justify the help menu automatically
  60.         if string.lower(label) == 'help' and parentmenu == self.__w:
  61.             item.right_justify()
  62.         parentmenu.append(item)
  63.         self.__items[path] = item
  64.         return item
  65.     def get_menu(self, path):
  66.         if path == '':
  67.             return self.__w
  68.         if self.__menus.has_key(path):
  69.             return self.__menus[path]
  70.         wid = self.create(path)
  71.         menu = GtkMenu()
  72.         menu.set_accel_group(self.accelerator)
  73.         wid.set_submenu(menu)
  74.         self.__menus[path] = menu
  75.         return menu
  76.     def parse_accelerator(self, accelerator):
  77.         key = 0
  78.         mods = 0
  79.         done = FALSE
  80.         while not done:
  81.             if accelerator[:7] == '<shift>':
  82.                 mods = mods | GDK.SHIFT_MASK
  83.                 accelerator = accelerator[7:]
  84.             elif accelerator[:5] == '<alt>':
  85.                 mods = mods | GDK.MOD1_MASK
  86.                 accelerator = accelerator[5:]
  87.             elif accelerator[:6] == '<meta>':
  88.                 mods = mods | GDK.MOD1_MASK
  89.                 accelerator = accelerator[6:]
  90.             elif accelerator[:9] == '<control>':
  91.                 mods = mods | GDK.CONTROL_MASK
  92.                 accelerator = accelerator[9:]
  93.             else:
  94.                 done = TRUE
  95.                 key = ord(accelerator[0])
  96.         return key, mods
  97.     def remove_entry(self, path):
  98.         if path not in self.__items.keys():
  99.             return
  100.         item = self.__items[path]
  101.         item.destroy()
  102.         length = len(path)
  103.         # clean up internal hashes
  104.         for i in self.__items.keys():
  105.             if i[:length] == path:
  106.                 del self.__items[i]
  107.         for i in self.__menus.keys():
  108.             if i[:length] == path:
  109.                 del self.__menus[i]
  110.     def remove_entries(self, paths):
  111.         for path in paths:
  112.             self.remove_entry(path)
  113.     def find(self, path):
  114.         return self.__items[path]
  115.  
  116. class _MessageBox(GtkDialog):
  117.     def __init__(self, message="", buttons=(), pixmap=None, modal=TRUE):
  118.         GtkDialog.__init__(self)
  119.         self.connect("destroy", self.quit)
  120.         self.connect("delete_event", self.quit)
  121.         if modal:
  122.             grab_add(self)
  123.         hbox = GtkHBox(spacing=5)
  124.         hbox.set_border_width(5)
  125.         self.vbox.pack_start(hbox)
  126.         hbox.show()
  127.         if pixmap:
  128.             self.realize()
  129.             pixmap = GtkPixmap(self, pixmap)
  130.             hbox.pack_start(pixmap, expand=FALSE)
  131.             pixmap.show()
  132.         label = GtkLabel(message)
  133.         hbox.pack_start(label)
  134.         label.show()
  135.  
  136.         for text in buttons:
  137.             b = GtkButton(text)
  138.             b.set_flags(CAN_DEFAULT)
  139.             b.set_data("user_data", text)
  140.             b.connect("clicked", self.click)
  141.             self.action_area.pack_start(b)
  142.             b.show()
  143.         self.ret = None
  144.     def quit(self, *args):
  145.         self.hide()
  146.         self.destroy()
  147.         mainquit()
  148.     def click(self, button):
  149.         self.ret = button.get_data("user_data")
  150.         self.quit()
  151.  
  152. # create a message box, and return which button was pressed        
  153. def message_box(title="Message Box", message="", buttons=(), pixmap=None,
  154.         modal=TRUE):
  155.     win = _MessageBox(message, buttons, pixmap=pixmap, modal=modal)
  156.     win.set_title(title)
  157.     win.show()
  158.     mainloop()
  159.     return win.ret
  160.  
  161. class _EntryDialog(GtkDialog):
  162.     def __init__(self, message="", modal=TRUE):
  163.         GtkDialog.__init__(self)
  164.         self.connect("destroy", self.quit)
  165.         self.connect("delete_event", self.quit)
  166.         if modal:
  167.             grab_add(self)
  168.         box = GtkVBox(spacing=10)
  169.         box.set_border_width(10)
  170.         self.vbox.pack_start(box)
  171.         box.show()
  172.         if message:
  173.             label = GtkLabel(message)
  174.             box.pack_start(label)
  175.             label.show()
  176.         self.entry = GtkEntry()
  177.         box.pack_start(self.entry)
  178.         self.entry.show()
  179.         self.entry.grab_focus()
  180.  
  181.         button = GtkButton("OK")
  182.         button.connect("clicked", self.click)
  183.         button.set_flags(CAN_DEFAULT)
  184.         self.action_area.pack_start(button)
  185.         button.show()
  186.         button.grab_default()
  187.         button = GtkButton("Cancel")
  188.         button.connect("clicked", self.quit)
  189.         button.set_flags(CAN_DEFAULT)
  190.         self.action_area.pack_start(button)
  191.         button.show()
  192.  
  193.         self.ret = None
  194.     def quit(self, w=None, event=None):
  195.         self.hide()
  196.         self.destroy()
  197.         mainquit()
  198.     def click(self, button):
  199.         self.ret = self.entry.get_text()
  200.         self.quit()
  201.  
  202. def input_box(title="Input Box", message="", modal=TRUE):
  203.     win = _EntryDialog(message, modal=modal)
  204.     win.set_title(title)
  205.     win.show()
  206.     mainloop()
  207.     return win.ret
  208.  
  209. # File selection helper functions
  210. class _FileSelection(GtkFileSelection):
  211.     def __init__(self, modal=TRUE):
  212.         GtkFileSelection.__init__(self)
  213.         self.connect("destroy", self.quit)
  214.         self.connect("delete_event", self.quit)
  215.         if modal:
  216.             grab_add(self)
  217.         self.cancel_button.connect('clicked', self.quit)
  218.         self.ok_button.connect('clicked', self.ok_cb)
  219.         self.ret = None
  220.     def quit(self, *args):
  221.         self.hide()
  222.         self.destroy()
  223.         mainquit()
  224.     def ok_cb(self, b):
  225.         self.ret = self.get_filename()
  226.         self.quit()
  227.  
  228. def file_sel_box(title="Browse", modal=FALSE):
  229.     win = _FileSelection(modal=modal)
  230.     win.set_title(title)
  231.     win.show()
  232.     mainloop()
  233.     return win.ret
  234.  
  235. def file_open_box(modal=TRUE):
  236.     return file_sel_box("Open", modal=modal)
  237. def file_save_box(modal=TRUE):
  238.     return file_sel_box("Save As", modal=modal)
  239.  
  240. def debug_main_quit():
  241.     """Create a window with a button to call mainquit"""
  242.         win = GtkWindow()
  243.     win.set_title("Quit")
  244.     win.set_usize(125, -1)
  245.     b = GtkButton("Main Quit")
  246.     b.connect("clicked", mainquit)
  247.     win.add(b)
  248.     b.show()
  249.     win.show()
  250.